package com.slt.android.customview.shaderview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.slt.android.customview.R;

/**
 * Created by dp on 2018/1/9.
 */

public class ShaderView extends View {

    Bitmap mBitmap , mSrcBitmap , mRefBitmap;
    BitmapShader mBitmapShader;
    Paint mPaint;
    PorterDuffXfermode mXfermode;
    public ShaderView(Context context) {
        super(context);
        init(context);
    }

    public ShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
            //BitmapShader
//        mBitmap = BitmapFactory.decodeResource(getResources() , R.mipmap.ic_launcher);
//        mBitmapShader = new BitmapShader(mBitmap , Shader.TileMode.MIRROR , Shader.TileMode.MIRROR);
//        mPaint = new Paint();
//        mPaint.setShader(mBitmapShader);

        //LinearGrident
//        mPaint = new Paint();
//        mPaint.setShader(new LinearGradient( 0 , 0 , 400 , 400 , Color.BLUE , Color.GREEN , Shader.TileMode.REPEAT));
        //LinearGrident + porterDuffXfermode
        mSrcBitmap = BitmapFactory.decodeResource(getResources() , R.mipmap.gxt );
        Matrix matrix = new Matrix();
        matrix.setScale(1f , -1f);//将图像进行翻转
        mRefBitmap = Bitmap.createBitmap(mSrcBitmap , 0 , 0 , mSrcBitmap.getWidth() , mSrcBitmap.getHeight() , matrix , true);
        mPaint = new Paint();
        //修改倒影渐变的范围 改这个shader
        mPaint.setShader(new LinearGradient(0 , mSrcBitmap.getHeight() , 0 , mSrcBitmap.getHeight() + mSrcBitmap.getHeight()/4 ,
                0xDD000000 , 0x10000000 , Shader.TileMode.CLAMP));

        mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);




    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //BitmapShader
//        canvas.drawCircle(500 , 500 , 550 , mPaint);
        //LinearGrident
//        canvas.drawRect(0 , 0 , 400 , 400 , mPaint);
        //LinearGrident + porterDuffXfermode
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mSrcBitmap , 0 , 0 , null);
        canvas.drawBitmap(mRefBitmap , 0 , mSrcBitmap.getHeight() , null);
        mPaint.setXfermode(mXfermode);
        canvas.drawRect(0 , mSrcBitmap.getHeight() , mRefBitmap.getWidth() , mSrcBitmap.getHeight() * 2 , mPaint);
        mPaint.setXfermode(null);

    }
}
